import json
import os

referenceFilePath = ''
# referenceFilePath = '/Users/far/Desktop/AllStock_sh.txt'
# testedFilePath = '/Users/far/Desktop/AllStock_sh的副本.txt'


def totalMatching()->bool:
    with open(file=referenceFilePath, mode='r', encoding='utf-8') as a:
        str_reference = a.read()
    with open(file=testedFilePath, mode='r', encoding='utf-8') as a:
        str_tested = a.read()
    return str_tested == str_reference

def stockMatching():
    key_reference = []
    key_tested = []
    dict_reference = {}
    dict_tested = {}
    falseStocks = []
    defectStocks_reference = []
    defectStocks_tested = []
    with open(file=referenceFilePath, mode='r', encoding='utf-8') as a:
        list_reference = json.loads(a.read())
        for temp in list_reference:
            dict_reference[f"{temp['s']}_{temp['p']}"] = temp
            key_reference.append(f"{temp['s']}_{temp['p']}")
        # print('%s' % key_reference)

    with open(file=testedFilePath, mode='r', encoding='utf-8') as a:
        list_tested = json.loads(a.read())
        for temp in list_tested:
            dict_tested[f"{temp['s']}_{temp['p']}"] = temp
            key_tested.append(f"{temp['s']}_{temp['p']}")

    for key in key_reference:
        if key in key_tested:
            pass
        else:
            defectStocks_reference.append(key)

    for key in key_tested:
        if key in key_reference:
            pass
        else:
            defectStocks_tested.append(key)

    for key in key_reference:
        if key in dict_tested.keys():
            if dict_tested[key] != dict_reference[key]:
                falseStocks.append(key)
            else:
                pass

    if defectStocks_reference != [] or defectStocks_tested != []:
        if defectStocks_reference != []:
            print('相对于reference，tested中缺失股票：',defectStocks_reference)
        if defectStocks_tested != []:
            print('相对于tested，reference中缺失股票：',defectStocks_tested)
    else:
        print('两端股名完全匹配！')

    if falseStocks != []:
        print('不一致股票信息如下：')
        for stock in falseStocks:
            print('%s:\nreference：%s\ntested：%s；\n' % (stock,dict_reference[stock],dict_tested[stock]))
    else:
        print('未发现不一致股票信息！')



while(referenceFilePath != 'exit' and referenceFilePath != 'EXIT'):
    referenceFilePath = input('请输入参考文件地址(输入exit退出测试)：').rstrip()

    if os.path.exists(referenceFilePath):
        while(1):
            testedFilePath = input('请输入被测文件地址(输入back回退至上一级，输入exit退出测试)：').rstrip()
            if testedFilePath == 'back' or testedFilePath == 'BACK':
                break
            if testedFilePath == 'exit' or testedFilePath == 'EXIT':
                referenceFilePath = 'exit'
                break
            if os.path.exists(testedFilePath):
                if totalMatching():
                    print('两个文件完全一致！')
                    print(
                        '------------------------------------------------------------------------------------------------')
                    break
                else:
                    print('文件比较中，请稍候......\n')
                    stockMatching()
                    print(
                        '------------------------------------------------------------------------------------------------')
                    break
            else:
                print('--找不到输入的被测文件！--')
    else:
        print('--找不到输入的参考文件！--')


